package com.dongdongshop.service.impl;

import com.dongdongshop.data.PageResult;
import com.dongdongshop.data.PageUtil;
import com.dongdongshop.domain.model.TbItemCat;
import com.dongdongshop.domain.model.TbItemCatExample;
import com.dongdongshop.domain.vo.ItemCatVO;
import com.dongdongshop.domain.vo.TypeTemplateVO;
import com.dongdongshop.mapper.TbItemCatMapper;
import com.dongdongshop.mapper.TbTypeTemplateMapper;
import com.dongdongshop.service.ItemCatService;
import com.dongdongshop.service.TypeTemplateService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class ItemCatServiceImpl implements ItemCatService {

    @Autowired
    private TbItemCatMapper catMapper;

    @Autowired
    private TbTypeTemplateMapper templateMapper;

    @Autowired
    private TypeTemplateService templateService;

    @Override
    public List<ItemCatVO> catList(Long parentId) {
        TbItemCatExample example = new TbItemCatExample();
        example.createCriteria().andParentIdEqualTo(parentId);
        List<TbItemCat> itemCatList = catMapper.selectByExample(example);
        return itemCatList.stream().map(tbItemCat -> {
            ItemCatVO vo = new ItemCatVO();
            BeanUtils.copyProperties(tbItemCat, vo);
            return vo;
        }).collect(Collectors.toList());
    }

    @Override
    public void addItemCat(ItemCatVO itemCat) {
        TbItemCat tbItemCat = new TbItemCat();
        BeanUtils.copyProperties(itemCat,tbItemCat);
        catMapper.insertSelective(tbItemCat);
    }

    @Override
    public ItemCatVO selectById(Long id) {
        TbItemCat tbItemCat = catMapper.selectByPrimaryKey(id);
        ItemCatVO vo = new ItemCatVO();
        BeanUtils.copyProperties(tbItemCat,vo);
        return vo;
    }

    @Override
    public void updateItemCat(ItemCatVO itemCat) {
        TbItemCat tbItemCat = new TbItemCat();
        BeanUtils.copyProperties(itemCat,tbItemCat);
        catMapper.updateByPrimaryKeySelective(tbItemCat);
    }

    @Override
    public void delete(List<Long> ids) {
        //删除主键为ids的数据
        TbItemCatExample example = new TbItemCatExample();
        example.createCriteria().andIdIn(ids);
        catMapper.deleteByExample(example);
        //删除父id为ids的数据
        TbItemCatExample example2 = new TbItemCatExample();
        example2.createCriteria().andParentIdIn(ids);
        catMapper.deleteByExample(example2);

    }

    @Override
    public String selectBrandId(Long id) {
        //通过主键查询到模板id
        TbItemCat tbItemCat = catMapper.selectByPrimaryKey(id);
        Long typeId = tbItemCat.getTypeId();
        //通过模板id查询到对应的品牌
        return templateMapper.selectByTypeId(typeId);
    }

    @Override
    public TypeTemplateVO getTemplateById(Long id) {
        TbItemCat itemCat = catMapper.selectByPrimaryKey(id);
        TypeTemplateVO templateVO = templateService.getByItemCatId(itemCat.getTypeId());
        return templateVO;
    }
}
